package com.umfwechat.h5.controller.settlement;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.constant.MsgConsts;
import com.umfwechat.common.constant.ValueConsts;
import com.umfwechat.h5.vo.RevPay;
import com.umfwechat.h5.vo.RevPayOrderNew;
import com.umfwechat.util.DateTimesUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.ResultJsonUtil;
import com.umfwechat.util.StringUtil;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.revpay.RevPayOrder;
import com.umpay.sp.service.revpaynew.remote.RevPayOrderServiceRemote;
import com.umpay.sp.util.AmountUtil;



/**
 * 查询商户结算列表数据（正常结算列表、提前结算列表）
 * @author css
 *
 */
@Controller
@RequestMapping("h5/queryPayController")
public class QueryPayController {

	private static Logger logger = LoggerFactory.getLogger(QueryPayController.class);
	
	//查询商户提现接口(sp)
	@Resource
	RevPayOrderServiceRemote revPayOrderServiceRemote;
	
	//查询商户提前结算列表接口（op）
	@Resource
	com.umpay.operation.service.revpay.remote.RevPayOrderServiceRemote opRevPayOrderServiceRemote;
	
	/**
	 * 查询商户正常结算接口
	 * @param request
	 * @param response
	 * @param merId
	 * @param pageNo
	 * @param currentPage
	 * @return
	 */
 	@RequestMapping(value="/queryRevPayOrder",method = {RequestMethod.POST,RequestMethod.GET },produces="text/html;charset=UTF-8")
 	@ResponseBody
	public String queryRevPayOrder(HttpServletRequest request, HttpServletResponse response ,String merId ,String pageNo ,String currentPage ){
		logger.info("【查询商户正常结算列表接口】开始");
		try {
			//校验前台传参
			if(StringUtil.isEmpty(merId)){
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(pageNo)){
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(currentPage)){
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~查询商户提现接口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			ReqMessage req = ReqMessageUtil.makeSpReq();
			Map<String, String> reqMap = new HashMap<String, String>();
			Calendar caEnd = Calendar.getInstance();//得到一个Calendar的实例 
			caEnd.setTime(new Date()); //设置时间为当前时间 
			Date end = caEnd.getTime();
			String endStr = DateTimesUtil.date8(end);
//			caEnd.add(Calendar.YEAR, -1); //年份减1 
			caEnd.add(Calendar.MONTH, -3 );
//			caEnd.set(Calendar.DATE, 5);
			Date  start  = caEnd.getTime(); //结果
			String startStr = DateTimesUtil.date8(start);
			logger.info("开始时间为：" + startStr);
//			System.out.println(startStr);
			reqMap.put(CommonConsts.MERID, merId);
			reqMap.put(CommonConsts.ORDERSTATE, ValueConsts.APP_CHANNEL_STATE_FOUR);//提现只查询成功的 4 
			reqMap.put(CommonConsts.END_DATE, endStr);
			reqMap.put(CommonConsts.START_DATE, startStr);
			reqMap.put(CommonConsts.CURRENT_PAGE , currentPage);
			reqMap.put(CommonConsts.PAGE_SIZE, pageNo);
			//················---正常结算产品（T+1）号---·························
			reqMap.put(CommonConsts.PRODUCTID, ValueConsts.PRODUCTID_SETTLE);
			//······························································
			req.setReqMap(reqMap);
			logger.info("【查询收入提现接口】入参：" + req);
			ResMessage res = revPayOrderServiceRemote.queryRevPayOrdersForPage(req);
			logger.info("【查询收入提现接口】出参：" + res);
			if(!res.getRetCode().equals(RetCode.SUCCESS) ){
				logger.info("付款查询失败！");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433075);
			}
			List<RevPayOrder> revPayOrderList = (List<RevPayOrder>) res.getResMap().get(CommonConsts.RESULT_LIST);
			List<RevPay> newRevPayOrderList = new ArrayList<RevPay>(); 
			Map<String, Object> resultMap = new HashMap<String , Object>();
			if(revPayOrderList == null || revPayOrderList.size() == 0){
				logger.info("查询商户正常结算（提现）信息无数据！");
				resultMap.put(CommonConsts.RESULTLIST,newRevPayOrderList);			
				return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.E00433112 ,resultMap);
			}
			for(RevPayOrder revPayOrder  : revPayOrderList){
				RevPay revPayNew = new RevPay();
				long time = revPayOrder.getInTime().getTime();
				Date t = new Date(time);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				revPayNew.setInTime(sdf.format(t));
//				revPayNew.setInTime(revPayOrder.getInTime());
				revPayNew.setAmount(AmountUtil.Cent2Dollar(revPayOrder.getAmount()));
				revPayNew.setInMediaId(revPayOrder.getInMediaId());
				revPayNew.setBankName(revPayOrder.getBankName());
				newRevPayOrderList.add(revPayNew);
			}
			resultMap.put(CommonConsts.RESULTLIST,newRevPayOrderList);			
			resultMap.put(CommonConsts.CURRENT_PAGE,currentPage);	
			logger.info("查询商户正常打款列表成功！");
			return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.I00433076,resultMap);
		} catch (Exception e) {
			logger.info("查询商户正常打款列表失败！");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
	}
 	
 	/**
 	 * 查询商户提前结算列表接口
 	 * @param request
 	 * @param response
 	 * @param merId
 	 * @param pageNo
 	 * @param currentPage
 	 * @return
 	 */
 	@RequestMapping(value="/queryEarlyPay",method = {RequestMethod.POST,RequestMethod.GET },produces="text/html;charset=UTF-8")
 	@ResponseBody
 	public String queryEarlyPay(HttpServletRequest request, HttpServletResponse response ,String merId ,String pageNo,String currentPage){
 		logger.info("【查询提前结算列表接口】开始");
 		try {
			if(StringUtil.isEmpty(merId)){
				logger.info("获取前台产值merId为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(pageNo)){
				logger.info("获取前台产值pageNo为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(currentPage)){
				logger.info("获取前台产值currentPage为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~查询商户提前结算列表接口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			com.umpay.operation.common.ReqMessage req = ReqMessageUtil.makeOpReq();
			Map<String, String> reqMap = new HashMap<String ,String>();
			
			Calendar caEnd = Calendar.getInstance();//得到一个Calendar的实例 
			caEnd.setTime(new Date()); //设置时间为当前时间 
			Date end = caEnd.getTime();
			String endStr = DateTimesUtil.date8(end);
			logger.info("查询提前结算列表结束时间为：" + endStr);
			System.out.println(endStr);
			
			caEnd.add(Calendar.YEAR, -1); //年份减1 
//			caEnd.add(Calendar.MONTH, -3 );
//			caEnd.set(Calendar.DATE, 5);
			Date  start  = caEnd.getTime(); //结果
			String startStr = DateTimesUtil.date8(start);
			logger.info("查询提前结算列表开始时间为：" + startStr);
//			System.out.println(startStr);
			
			reqMap.put(CommonConsts.END_DATE, endStr);
			reqMap.put(CommonConsts.START_DATE, startStr);
			reqMap.put(CommonConsts.MERID, merId);
			reqMap.put(CommonConsts.BUSIID,ValueConsts.BUSIID);
			reqMap.put(CommonConsts.CURRENT_PAGE , currentPage);
			reqMap.put(CommonConsts.PAGE_SIZE, pageNo);
			reqMap.put("pageFlag", "1");
			req.put(reqMap);
			logger.info("【查询提前结算列表接口】入参：" + req);
			com.umpay.operation.common.ResMessage res = opRevPayOrderServiceRemote.queryCbePayOrderList(req);
			logger.info("【查询提前结算列表接口】出参：" + res);
			if(!res.getRetCode().equals(RetCode.SUCCESS)){
				logger.info("查询提前结算列表接口失败");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433016);
			}
			List<com.umpay.operation.model.revpay.RevPayOrder> revPayOrderResultList = (List<com.umpay.operation.model.revpay.RevPayOrder>) res.getResMap().get(CommonConsts.RESULT_LIST);
			Map<String , Object> resMap = new HashMap<String ,Object>();
			List<RevPayOrderNew>  resultList = new ArrayList<RevPayOrderNew>();
			if(revPayOrderResultList == null || revPayOrderResultList.size() == 0 ){
				logger.info("查询提前结算接口无数据");
				resMap.put(CommonConsts.RESULT_LIST, resultList);
				return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.E00433119 ,resMap);
			}
			for(com.umpay.operation.model.revpay.RevPayOrder revPayOrder : revPayOrderResultList ){
				RevPayOrderNew revPayOrderNew = new RevPayOrderNew();
				long time = revPayOrder.getInTime().getTime();
				Date d = new Date(time);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
				revPayOrderNew.setInTime(sdf.format(d));
//				revPayOrderNew.setInTime(revPayOrder.getInTime());
				revPayOrderNew.setOrderState(revPayOrder.getOrderState());
//				revPayOrderNew.setAmount(AmountUtil.Cent2Dollar(revPayOrder.getAmount()));
				revPayOrderNew.setOrigAmt(AmountUtil.Cent2Dollar(revPayOrder.getOrigAmt()));
				revPayOrderNew.setComAmt(AmountUtil.Cent2Dollar(revPayOrder.getComAmt()));
				revPayOrderNew.setTradeDesc(revPayOrder.getTradeDesc());
				revPayOrderNew.setOrderId(revPayOrder.getOrderId());
				resultList.add(revPayOrderNew);
			}
			resMap.put(CommonConsts.RESULT_LIST, resultList);
			resMap.put(CommonConsts.CURRENT_PAGE, currentPage);
			logger.info("查询商户提前结算列表成功！");
			return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.I00433113 ,resMap);
		} catch (Exception e) {
			logger.info("查询提前结算列表失败");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
 	}
}
